package cn.ibizlab.odoo.core.odoo_mail.service.impl;

import java.io.Serializable;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.HashMap;
import java.math.BigInteger;
import java.util.Collection;

import lombok.extern.slf4j.Slf4j;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cglib.beans.BeanCopier;
import org.springframework.stereotype.Service;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;
import cn.ibizlab.odoo.core.client.model.Imail_mail_statistics;
import cn.ibizlab.odoo.core.odoo_mail.domain.Mail_mail_statistics;
import cn.ibizlab.odoo.core.odoo_mail.filter.Mail_mail_statisticsSearchContext;
import cn.ibizlab.odoo.core.odoo_mail.service.IMail_mail_statisticsService;
import cn.ibizlab.odoo.util.SearchResult;
import cn.ibizlab.odoo.util.log.IBIZLog;

/**
 * 实体[邮件统计] 服务对象接口实现
 */
@Slf4j
@IBIZLog
@Service
public class Mail_mail_statisticsServiceImpl implements IMail_mail_statisticsService {

    @Autowired
    private cn.ibizlab.odoo.core.client.service.Imail_mail_statisticsClientService mail_mail_statisticsClientService;

    @Override
    public boolean update(Mail_mail_statistics mail_mail_statistics) {
        boolean bOk = false ;
        Imail_mail_statistics clientModel = this.convert2Model(mail_mail_statistics,null);
        mail_mail_statisticsClientService.update(clientModel) ;
        this.convert2Domain(clientModel,mail_mail_statistics) ;
        bOk = true;
        return bOk ;
    }

    @Override
    public Mail_mail_statistics get(Integer id) {
        Imail_mail_statistics clientModel = mail_mail_statisticsClientService.createModel();
        clientModel.setId(id);
        mail_mail_statisticsClientService.get(clientModel) ;
        return this.convert2Domain(clientModel,null);
    }

    @Override
    public Mail_mail_statistics get(Mail_mail_statistics mail_mail_statistics) {
        Mail_mail_statistics retmail_mail_statistics = this.get(mail_mail_statistics.getId()) ;
    	BeanCopier copier=BeanCopier.create(retmail_mail_statistics.getClass(), mail_mail_statistics.getClass(), false);
        copier.copy(retmail_mail_statistics, mail_mail_statistics, null);
        return mail_mail_statistics ;
    }

    @Override
    public void updateBatch(List<Mail_mail_statistics> mail_mail_statisticss, int batchSize) {

        //mail_mail_statisticsClientService.updateBatch(mail_mail_statisticss,batchSize) ;

    }

    @Override
    public void removeBatch(Collection<? extends Serializable> idList, int batchSize) {

        //mail_mail_statisticsClientService.removeBatch(mail_mail_statisticss) ;

    }

    @Override
    public void createBatch(List<Mail_mail_statistics> mail_mail_statisticss, int batchSize) {

                        //mail_mail_statisticsClientService.createBatch(mail_mail_statisticss,batchSize) ;

    }

    @Override
    public boolean remove(Integer id) {
        boolean bOk = false ;
        Imail_mail_statistics clientModel = mail_mail_statisticsClientService.createModel();
        clientModel.setId(id);
        mail_mail_statisticsClientService.remove(clientModel) ;
        bOk = true ;
        return bOk ;
    }

    @Override
    public boolean create(Mail_mail_statistics mail_mail_statistics) {
        boolean bOk = false ;
        Imail_mail_statistics clientModel = this.convert2Model(mail_mail_statistics,null);
        mail_mail_statisticsClientService.create(clientModel) ;
        this.convert2Domain(clientModel,mail_mail_statistics);
        bOk = true ;

        return bOk ;
    }

    /**
     * 查询集合 默认查询
     */
    @Override
    public Page<Mail_mail_statistics> searchDefault(Mail_mail_statisticsSearchContext context) {
        List<Mail_mail_statistics> list = new ArrayList<Mail_mail_statistics>();
        Page<cn.ibizlab.odoo.core.client.model.Imail_mail_statistics> clientModelList = mail_mail_statisticsClientService.search(context) ;
        for(cn.ibizlab.odoo.core.client.model.Imail_mail_statistics clientModel : clientModelList){
            list.add( this.convert2Domain(clientModel,null));
        }
        return new PageImpl<Mail_mail_statistics>(list, context.getPageable(), clientModelList.getTotalElements());
    }


    /**
     * 根据寄出邮件查询
     */
    @Override
    public Page<Mail_mail_statistics> selectByOdoo_mail_mail(Integer id){
        Mail_mail_statisticsSearchContext context = new Mail_mail_statisticsSearchContext();
        if(!org.springframework.util.StringUtils.isEmpty(id)){
            cn.ibizlab.odoo.util.SearchFieldFilter fieldFilter=new cn.ibizlab.odoo.util.SearchFieldFilter();
            fieldFilter.setParam("id");
            fieldFilter.setCondition(cn.ibizlab.odoo.util.enums.SearchFieldType.EQ);
            fieldFilter.setValue(id);
            context.getCondition().add(fieldFilter);
        }
        List<Mail_mail_statistics> list = new ArrayList<Mail_mail_statistics>();
        Page<cn.ibizlab.odoo.core.client.model.Imail_mail_statistics> clientModelList = mail_mail_statisticsClientService.search(context) ;
        for(cn.ibizlab.odoo.core.client.model.Imail_mail_statistics clientModel : clientModelList){
            list.add( this.convert2Domain(clientModel,null));
        }
        return new PageImpl<Mail_mail_statistics>(list, context.getPageable(), clientModelList.getTotalElements());

    }

    /**
     * 根据群发邮件营销查询
     */
    @Override
    public Page<Mail_mail_statistics> selectByOdoo_mass_mailing_campaign(Integer id){
        Mail_mail_statisticsSearchContext context = new Mail_mail_statisticsSearchContext();
        if(!org.springframework.util.StringUtils.isEmpty(id)){
            cn.ibizlab.odoo.util.SearchFieldFilter fieldFilter=new cn.ibizlab.odoo.util.SearchFieldFilter();
            fieldFilter.setParam("id");
            fieldFilter.setCondition(cn.ibizlab.odoo.util.enums.SearchFieldType.EQ);
            fieldFilter.setValue(id);
            context.getCondition().add(fieldFilter);
        }
        List<Mail_mail_statistics> list = new ArrayList<Mail_mail_statistics>();
        Page<cn.ibizlab.odoo.core.client.model.Imail_mail_statistics> clientModelList = mail_mail_statisticsClientService.search(context) ;
        for(cn.ibizlab.odoo.core.client.model.Imail_mail_statistics clientModel : clientModelList){
            list.add( this.convert2Domain(clientModel,null));
        }
        return new PageImpl<Mail_mail_statistics>(list, context.getPageable(), clientModelList.getTotalElements());

    }

    /**
     * 根据群发邮件查询
     */
    @Override
    public Page<Mail_mail_statistics> selectByOdoo_mass_mailing(Integer id){
        Mail_mail_statisticsSearchContext context = new Mail_mail_statisticsSearchContext();
        if(!org.springframework.util.StringUtils.isEmpty(id)){
            cn.ibizlab.odoo.util.SearchFieldFilter fieldFilter=new cn.ibizlab.odoo.util.SearchFieldFilter();
            fieldFilter.setParam("id");
            fieldFilter.setCondition(cn.ibizlab.odoo.util.enums.SearchFieldType.EQ);
            fieldFilter.setValue(id);
            context.getCondition().add(fieldFilter);
        }
        List<Mail_mail_statistics> list = new ArrayList<Mail_mail_statistics>();
        Page<cn.ibizlab.odoo.core.client.model.Imail_mail_statistics> clientModelList = mail_mail_statisticsClientService.search(context) ;
        for(cn.ibizlab.odoo.core.client.model.Imail_mail_statistics clientModel : clientModelList){
            list.add( this.convert2Domain(clientModel,null));
        }
        return new PageImpl<Mail_mail_statistics>(list, context.getPageable(), clientModelList.getTotalElements());

    }

    /**
     * 根据用户查询
     */
    @Override
    public Page<Mail_mail_statistics> selectByOdoo_create(Integer id){
        Mail_mail_statisticsSearchContext context = new Mail_mail_statisticsSearchContext();
        if(!org.springframework.util.StringUtils.isEmpty(id)){
            cn.ibizlab.odoo.util.SearchFieldFilter fieldFilter=new cn.ibizlab.odoo.util.SearchFieldFilter();
            fieldFilter.setParam("id");
            fieldFilter.setCondition(cn.ibizlab.odoo.util.enums.SearchFieldType.EQ);
            fieldFilter.setValue(id);
            context.getCondition().add(fieldFilter);
        }
        List<Mail_mail_statistics> list = new ArrayList<Mail_mail_statistics>();
        Page<cn.ibizlab.odoo.core.client.model.Imail_mail_statistics> clientModelList = mail_mail_statisticsClientService.search(context) ;
        for(cn.ibizlab.odoo.core.client.model.Imail_mail_statistics clientModel : clientModelList){
            list.add( this.convert2Domain(clientModel,null));
        }
        return new PageImpl<Mail_mail_statistics>(list, context.getPageable(), clientModelList.getTotalElements());

    }

    /**
     * 根据用户查询
     */
    @Override
    public Page<Mail_mail_statistics> selectByOdoo_write(Integer id){
        Mail_mail_statisticsSearchContext context = new Mail_mail_statisticsSearchContext();
        if(!org.springframework.util.StringUtils.isEmpty(id)){
            cn.ibizlab.odoo.util.SearchFieldFilter fieldFilter=new cn.ibizlab.odoo.util.SearchFieldFilter();
            fieldFilter.setParam("id");
            fieldFilter.setCondition(cn.ibizlab.odoo.util.enums.SearchFieldType.EQ);
            fieldFilter.setValue(id);
            context.getCondition().add(fieldFilter);
        }
        List<Mail_mail_statistics> list = new ArrayList<Mail_mail_statistics>();
        Page<cn.ibizlab.odoo.core.client.model.Imail_mail_statistics> clientModelList = mail_mail_statisticsClientService.search(context) ;
        for(cn.ibizlab.odoo.core.client.model.Imail_mail_statistics clientModel : clientModelList){
            list.add( this.convert2Domain(clientModel,null));
        }
        return new PageImpl<Mail_mail_statistics>(list, context.getPageable(), clientModelList.getTotalElements());

    }



    public Imail_mail_statistics convert2Model(Mail_mail_statistics domain , Imail_mail_statistics model) {
        if(domain == null)
            return model ;
        if(model==null){
            model = mail_mail_statisticsClientService.createModel() ;
        }

         if(domain.getScheduledDirtyFlag())
            model.setScheduled(domain.getScheduled());
         if(domain.getClickedDirtyFlag())
            model.setClicked(domain.getClicked());
         if(domain.getDisplay_nameDirtyFlag())
            model.setDisplay_name(domain.getDisplay_name());
         if(domain.getEmailDirtyFlag())
            model.setEmail(domain.getEmail());
         if(domain.getCreate_dateDirtyFlag())
            model.setCreate_date(domain.getCreate_date());
         if(domain.getState_updateDirtyFlag())
            model.setState_update(domain.getState_update());
         if(domain.getRepliedDirtyFlag())
            model.setReplied(domain.getReplied());
         if(domain.getWrite_dateDirtyFlag())
            model.setWrite_date(domain.getWrite_date());
         if(domain.getModelDirtyFlag())
            model.setModel(domain.getModel());
         if(domain.getMessage_idDirtyFlag())
            model.setMessage_id(domain.getMessage_id());
         if(domain.getExceptionDirtyFlag())
            model.setException(domain.getException());
         if(domain.get__last_updateDirtyFlag())
            model.set__last_update(domain.get__last_update());
         if(domain.getBouncedDirtyFlag())
            model.setBounced(domain.getBounced());
         if(domain.getIgnoredDirtyFlag())
            model.setIgnored(domain.getIgnored());
         if(domain.getRes_idDirtyFlag())
            model.setRes_id(domain.getRes_id());
         if(domain.getSentDirtyFlag())
            model.setSent(domain.getSent());
         if(domain.getMail_mail_id_intDirtyFlag())
            model.setMail_mail_id_int(domain.getMail_mail_id_int());
         if(domain.getLinks_click_idsDirtyFlag())
            model.setLinks_click_ids(domain.getLinks_click_ids());
         if(domain.getIdDirtyFlag())
            model.setId(domain.getId());
         if(domain.getStateDirtyFlag())
            model.setState(domain.getState());
         if(domain.getOpenedDirtyFlag())
            model.setOpened(domain.getOpened());
         if(domain.getCreate_uid_textDirtyFlag())
            model.setCreate_uid_text(domain.getCreate_uid_text());
         if(domain.getWrite_uid_textDirtyFlag())
            model.setWrite_uid_text(domain.getWrite_uid_text());
         if(domain.getMass_mailing_campaign_id_textDirtyFlag())
            model.setMass_mailing_campaign_id_text(domain.getMass_mailing_campaign_id_text());
         if(domain.getMass_mailing_id_textDirtyFlag())
            model.setMass_mailing_id_text(domain.getMass_mailing_id_text());
         if(domain.getWrite_uidDirtyFlag())
            model.setWrite_uid(domain.getWrite_uid());
         if(domain.getMass_mailing_campaign_idDirtyFlag())
            model.setMass_mailing_campaign_id(domain.getMass_mailing_campaign_id());
         if(domain.getMail_mail_idDirtyFlag())
            model.setMail_mail_id(domain.getMail_mail_id());
         if(domain.getCreate_uidDirtyFlag())
            model.setCreate_uid(domain.getCreate_uid());
         if(domain.getMass_mailing_idDirtyFlag())
            model.setMass_mailing_id(domain.getMass_mailing_id());
        return model ;
    }

    public Mail_mail_statistics convert2Domain( Imail_mail_statistics model ,Mail_mail_statistics domain) {
        if(model == null)
            return domain ;
        if(domain == null){
            domain = new Mail_mail_statistics();
        }

        if(model.getScheduledDirtyFlag())
            domain.setScheduled(model.getScheduled());
        if(model.getClickedDirtyFlag())
            domain.setClicked(model.getClicked());
        if(model.getDisplay_nameDirtyFlag())
            domain.setDisplay_name(model.getDisplay_name());
        if(model.getEmailDirtyFlag())
            domain.setEmail(model.getEmail());
        if(model.getCreate_dateDirtyFlag())
            domain.setCreate_date(model.getCreate_date());
        if(model.getState_updateDirtyFlag())
            domain.setState_update(model.getState_update());
        if(model.getRepliedDirtyFlag())
            domain.setReplied(model.getReplied());
        if(model.getWrite_dateDirtyFlag())
            domain.setWrite_date(model.getWrite_date());
        if(model.getModelDirtyFlag())
            domain.setModel(model.getModel());
        if(model.getMessage_idDirtyFlag())
            domain.setMessage_id(model.getMessage_id());
        if(model.getExceptionDirtyFlag())
            domain.setException(model.getException());
        if(model.get__last_updateDirtyFlag())
            domain.set__last_update(model.get__last_update());
        if(model.getBouncedDirtyFlag())
            domain.setBounced(model.getBounced());
        if(model.getIgnoredDirtyFlag())
            domain.setIgnored(model.getIgnored());
        if(model.getRes_idDirtyFlag())
            domain.setRes_id(model.getRes_id());
        if(model.getSentDirtyFlag())
            domain.setSent(model.getSent());
        if(model.getMail_mail_id_intDirtyFlag())
            domain.setMail_mail_id_int(model.getMail_mail_id_int());
        if(model.getLinks_click_idsDirtyFlag())
            domain.setLinks_click_ids(model.getLinks_click_ids());
        if(model.getIdDirtyFlag())
            domain.setId(model.getId());
        if(model.getStateDirtyFlag())
            domain.setState(model.getState());
        if(model.getOpenedDirtyFlag())
            domain.setOpened(model.getOpened());
        if(model.getCreate_uid_textDirtyFlag())
            domain.setCreate_uid_text(model.getCreate_uid_text());
        if(model.getWrite_uid_textDirtyFlag())
            domain.setWrite_uid_text(model.getWrite_uid_text());
        if(model.getMass_mailing_campaign_id_textDirtyFlag())
            domain.setMass_mailing_campaign_id_text(model.getMass_mailing_campaign_id_text());
        if(model.getMass_mailing_id_textDirtyFlag())
            domain.setMass_mailing_id_text(model.getMass_mailing_id_text());
        if(model.getWrite_uidDirtyFlag())
            domain.setWrite_uid(model.getWrite_uid());
        if(model.getMass_mailing_campaign_idDirtyFlag())
            domain.setMass_mailing_campaign_id(model.getMass_mailing_campaign_id());
        if(model.getMail_mail_idDirtyFlag())
            domain.setMail_mail_id(model.getMail_mail_id());
        if(model.getCreate_uidDirtyFlag())
            domain.setCreate_uid(model.getCreate_uid());
        if(model.getMass_mailing_idDirtyFlag())
            domain.setMass_mailing_id(model.getMass_mailing_id());
        return domain ;
    }


}
